package com.py.payment.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.py.payment.dto.param.MonitorParamDto;
import com.py.payment.dto.ret.EarlyInfoRetDto;
import com.py.payment.entity.*;
import com.py.payment.service.*;
import com.py.payment.tools.date.DateUtils;
import com.py.payment.tools.response.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 预警
 */
@RestController
@RequestMapping("/earlyWarning")
@CrossOrigin(origins = "*")
public class EarlyWarningController {

    @Autowired
    private MonitorService monitorService;

    @Autowired
    private PointPositionService pointPositionService;

    @Autowired
    private MonitoringPointsService monitoringPointsService;

    @Autowired
    private EarlyInfoService earlyInfoService;

    @Autowired
    private EarlyLevelConfigService earlyLevelConfigService;

    @PostMapping(name = "获取状态",path = "/getStatus")
    public ResponseData getStatus(){
        List<Monitor> monitorList = this.monitorService.getStatus();
        Integer status = 1;
        String dateTime = DateUtils.dateToDateStr(new Date(),DateUtils.YYYY_MM_DD_HH_MM_SS);
        for (Monitor item : monitorList) {
            if(item.getActual().compareTo(item.getEarly())>0) {
                status = 2;
                dateTime = DateUtils.dateToDateStr(item.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS);
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("status",status);
        jsonObject.put("dateTime",dateTime);
        return ResponseData.success(jsonObject);
    }

    @PostMapping(name = "获取预警列表",path = "/getEarlyList")
    public ResponseData getEarlyList(){
        List<Monitor> monitorList = this.monitorService.getStatus();
        List<Integer> pointIdList = new ArrayList<>();
        for (Monitor item : monitorList) {
            if (item.getActual().compareTo(item.getEarly()) > 0) {
                pointIdList.add(item.getPointId());
            }
        }
        QueryWrapper<EarlyInfo> earlyInfoQueryWrapper = new QueryWrapper<>();
        earlyInfoQueryWrapper.in("point_id",pointIdList);
        earlyInfoQueryWrapper.orderByDesc("create_time");
        earlyInfoQueryWrapper.last("limit 15");
        List<EarlyInfo> list = this.earlyInfoService.list(earlyInfoQueryWrapper);
        List<EarlyInfoRetDto> earlyInfoRetDtoList = JSON.parseArray(JSON.toJSONString(list), EarlyInfoRetDto.class);
        earlyInfoRetDtoList.forEach(item->{
            item.setCreateTimeStr(DateUtils.dateToDateStr(item.getCreateTime(),DateUtils.YYYY_MM_DD_HH_MM_SS));
            PointPosition position = this.pointPositionService.getById(item.getMonitorType());
            item.setMonitorTypeTile(position.getTitle());
            MonitoringPoints monitoringPoints = this.monitoringPointsService.getById(item.getPointId());
            item.setPointName(monitoringPoints.getTitle());
        });
        return ResponseData.success(earlyInfoRetDtoList);
    }

    @PostMapping(name = "获取预警配置",path = "/getEarlyConfig")
    public ResponseData getEarlyConfig(){
        List<EarlyLevelConfig> list = this.earlyLevelConfigService.getEarlyConfigList();
        EarlyLevelConfig last = new EarlyLevelConfig();
        for (EarlyLevelConfig item : list) {
            if(item.getIsEdge()==2){
                last = item;
            } else{
                item.setVerificationValue(last.getVerificationValue());
            }
            item.setVerificationValue(item.getVerificationValue());
        }
        return ResponseData.success(list);
    }

    @PostMapping(name = "配置预警",path = "/setEarlyConfig")
    public ResponseData setEarlyConfig(@RequestBody List<EarlyLevelConfig> configList){
        List<EarlyLevelConfig> update = new ArrayList<>();
        for (EarlyLevelConfig config : configList) {
            if(config.getIsEdge()==2){
                EarlyLevelConfig earlyLevelConfig = new EarlyLevelConfig();
                earlyLevelConfig.setId(config.getId());
                BigDecimal verificationValue = config.getVerificationValue();
                earlyLevelConfig.setVerificationValue(verificationValue);
                earlyLevelConfig.setIsAlert(config.getIsAlert());
                update.add(earlyLevelConfig);
            }
        }
        this.earlyLevelConfigService.updateBatchById(update);
        return ResponseData.success();
    }

    @PostMapping(path = "/test")
    public ResponseData test(@RequestBody String param){
        List<MonitorParamDto> monitors = JSON.parseArray(param, MonitorParamDto.class);
        List<Monitor> monitorList = new ArrayList<>();
        for (MonitorParamDto item : monitors) {
            Monitor monitor = JSON.parseObject(JSON.toJSONString(item), Monitor.class);
            monitor.setCreateTime(DateUtils.datestrToDate(item.getTimeStr(),DateUtils.YYYY_MM_DD_HH_MM_SS));
            monitorList.add(monitor);
        }
        this.monitorService.addMonitor(monitorList);
        return ResponseData.success();
    }
}
